﻿using Hsp.Novels.DbUtility;
using Hsp.Novels.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hsp.Novels.Dal
{
    public class ContentDal : SugarBase
    {
        #region 根据章节编号获取章节内容数据

        /// <summary>
        /// 根据章节ID获取章节内容实体
        /// </summary>
        /// <param name="chapterId">章节编号</param>
        /// <returns></returns>
        public static Content ChapterModel(string chapterId)
        {
            return db.Queryable<Content>().First(c => c.ChapterId == chapterId);
        }

        /// <summary>
        /// 根据章节ID获取章节内容实体
        /// </summary>
        /// <param name="chapterId">章节编号</param>
        /// <returns></returns>
        public static Content ChapterModelByChapterId(string chapterId)
        {
            return db.Queryable<Content>().First(c => c.ChapterId == chapterId);
        }

        #endregion

        #region 添加内容数据

        /// <summary>
        /// 添加内容数据
        /// </summary>
        /// <remarks>创建人：李海玉   创建时间：2018-06-02</remarks>
        /// <param name="model">内容实体</param>
        /// <returns></returns>
        public static int Add(Content model)
        {
            return db.Insertable(model).ExecuteCommand();
        }

        #endregion

        #region 编辑内容数据

        /// <summary>
        /// 编辑内容数据
        /// </summary>
        /// <param name="model">内容实体</param>
        /// <returns></returns>
        public static int Edit(Content model)
        {
            return db.Updateable(model).ExecuteCommand();
        }

        #endregion

        #region 根据章节ID删除内容数据

        /// <summary>
        /// 根据章节ID删除内容数据
        /// </summary>
        /// <param name="id">内容编号</param>
        /// <returns></returns>
        public static int Delete(string id)
        {
            return db.Deleteable<Content>().Where(n => n.ChapterId == id).ExecuteCommand();
        }

        #endregion

        #region 根据小说ID删除内容数据

        /// <summary>
        /// 根据小说ID删除内容数据
        /// </summary>
        /// <param name="novelId">小说ID</param>
        /// <returns></returns>
        public static int BatchDelete(string novelId)
        {
            return db.Deleteable<Content>().Where(n => n.NovelId == novelId).ExecuteCommand();
        }

        #endregion

        #region 清空小说内容数据

        /// <summary>
        /// 清空小说内容数据
        /// </summary>
        /// <param name="novelId">小说编号</param>
        /// <returns></returns>
        public static int ClearContent(string novelId)
        {
            string strSql = string.Format(@"UPDATE dbo.Contents SET Contents = '' WHERE (NovelId = '{0}');", novelId);
            return DbHelperSql.ExecuteSql(strSql);
        }

        #endregion
    }
}
